//
//  VVAnimator.m
//  TestShapeLayer
//
//  Created by 王惠平 on 2017/4/23.
//  Copyright © 2017年 will wang. All rights reserved.
//

#import "VVAnimator.h"

@implementation VVAnimator {
    
    //转场上下文
    __weak id <UIViewControllerContextTransitioning> _transitionContext;
    
    //记录是否已经展示界面
    BOOL _hasPresented;
}

#pragma  mark -  UIViewControllerTransitioningDelegate 代理

//设置负责展示转场动画的对象时调用
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    
    _hasPresented = NO;
    
    return self;
    
}

//设置负责解除转场动画的对象时调用
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    
    //设置标记
    _hasPresented = YES;
    
    return  self;
}

#pragma mark - UIViewControllerAnimatedTransitioning

//设置转场的时长
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    return 1.f;
}

//实现转场动画时调用
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    //记录转场上下文
    _transitionContext = transitionContext;
    
    //获取目标控制器的视图
    UIView *destView = [transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *sourceView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    
    //获取转场容器视图
    UIView *containerView = [transitionContext containerView];
    
    if (!_hasPresented) {
        
        //进行自定义转场时,需要手动将目标视图添加到界面上
        [containerView addSubview:destView];
        
        destView.frame = containerView.bounds;
    }
    
    UIView *contentView = _hasPresented ? sourceView : destView;
    
    //在目标视图上设置遮罩动画
    [self custemAnimInView:contentView];
}

- (void)custemAnimInView:(UIView *)view {
    
    //创建形状视图
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    //起始位置
    //轨迹按钮原始尺寸30, 改成了40 差10个点 ,约束定义的的top28,right16 要获得真是的轨迹按钮与top,right的约束 分别加5个点,
    CGFloat topMargin = 33;
    CGFloat rightMargin = 21;
    CGFloat radius = 15;
    
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    
    CGRect soucreRect = CGRectMake(screenW - rightMargin - 2 * radius, topMargin, 2 * radius, 2 * radius);
    
    CGPathRef sourcePath = [UIBezierPath bezierPathWithOvalInRect:soucreRect].CGPath;
    
    //结束位置  半径^2 = screenW^2 + screenH^2
    //使用屏幕的斜边长度作为结束位置的半径
    CGFloat endRadius = sqrt((screenW * screenW) + (screenH * screenH));
    
    //用于对尺寸进行缩放,中心点不变  CGRectInset(原始尺寸, x方向缩小的维度, y方向缩小的维度) 正值缩小,负值放大
    CGRect endRect = CGRectInset(soucreRect, -endRadius, -endRadius);
    
    //设置形状
    CGPathRef  endPath = [UIBezierPath bezierPathWithOvalInRect:endRect].CGPath;
    
    //设置遮罩
    view.layer.mask = shapeLayer;
    
    //对layer设置属性动画,必须使用CA动画,不能使用UIView的块动画
    //创建基础动画
    CABasicAnimation *basicAnim = [CABasicAnimation animationWithKeyPath:@"path"];
    
    //设置动画始末值
    if (_hasPresented) {
        
        basicAnim.toValue = (__bridge id _Nullable)(sourcePath);
        basicAnim.fromValue = (__bridge id _Nullable)(endPath);
        
    } else {
        
        basicAnim.fromValue = (__bridge id _Nullable)(sourcePath);
        basicAnim.toValue = (__bridge id _Nullable)(endPath);
    }
    
    //设置时长 之前设置的转场时间为1.f
    basicAnim.duration = [self transitionDuration:_transitionContext];
    
    //保留动画最终的内容
    basicAnim.removedOnCompletion = NO;
    
    basicAnim.fillMode = kCAFillModeForwards;
    
    basicAnim.delegate = self;
    
    //添加动画  设置改变哪个对象的属性,就将动画添加到哪个对象上
    [shapeLayer addAnimation:basicAnim forKey:nil];

    
}

//pragma mark - CAAnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    //完成转场 如果不完成,无法响应交互事件
    [_transitionContext completeTransition:YES];
}

@end
